{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['序号', '性别', '年龄', '学历', '职业', '使用', 'Male'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "data = pd.read_excel('二元选择模型数据.xlsx')\n",
    "columns = data.columns\n",
    "print(columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 性别已经做好了\n",
    "Age = data['年龄']\n",
    "# 对于四值变量年龄，分别生成四个新变量, Age0_18, Age18_30, Age31_50, Age51_100\n",
    "Age0_18 = (Age == 1).astype(int)\n",
    "Age18_30 = (Age == 2).astype(int)\n",
    "Age31_50 = (Age == 3).astype(int)\n",
    "Age51_100 = (Age == 4).astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "#学历\n",
    "Education = data['学历']\n",
    "# 对于四值变量学历，分别生成四个新变量,Junior, Senior, College, Master\n",
    "Junior = (Education == 1).astype(int)\n",
    "Senior = (Education == 2).astype(int)\n",
    "College = (Education == 3).astype(int)\n",
    "Master = (Education == 4).astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "#职业\n",
    "Occupation = data['职业']\n",
    "# 对于七值变量职业，分别生成七个新变量,Job1, Job2, Job3, Job4, Job5, Job6, Job7\n",
    "Job1 = (Occupation == 1).astype(int)\n",
    "Job2 = (Occupation == 2).astype(int)\n",
    "Job3 = (Occupation == 3).astype(int)\n",
    "Job4 = (Occupation == 4).astype(int)\n",
    "Job5 = (Occupation == 5).astype(int)\n",
    "Job6 = (Occupation == 6).astype(int)\n",
    "Job7 = (Occupation == 7).astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "done\n"
     ]
    }
   ],
   "source": [
    "#将生成的新变量作为新列插入到excel表的末尾\n",
    "data.insert(7, 'Age0_18', Age0_18)\n",
    "data.insert(8, 'Age18_30', Age18_30)\n",
    "data.insert(9, 'Age31_50', Age31_50)\n",
    "data.insert(10, 'Age51_100', Age51_100)\n",
    "data.insert(11, 'Junior', Junior)\n",
    "data.insert(12, 'Senior', Senior)\n",
    "data.insert(13, 'College', College)\n",
    "data.insert(14, 'Master', Master)\n",
    "data.insert(15, 'Job1', Job1)\n",
    "data.insert(16, 'Job2', Job2)\n",
    "data.insert(17, 'Job3', Job3)\n",
    "data.insert(18, 'Job4', Job4)\n",
    "data.insert(19, 'Job5', Job5)\n",
    "data.insert(20, 'Job6', Job6)\n",
    "data.insert(21, 'Job7', Job7)\n",
    "\n",
    "#保存Excel\n",
    "data.to_excel('二元选择模型数据.xlsx', index=False)\n",
    "print('done')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
